package com.edu.hre.db.config;

/**
 * Created by IntelliJ IDEA.
 * User: Nguyen The Tung
 * Date: Dec 26, 2009
 * Time: 11:13:28 AM
 */
import org.apache.log4j.Logger;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class DBConfig {

    private static final Logger logger = Logger.getLogger(DBConfig.class);
    private static final String fileName0 = "/testscp/hre_mobile.cfg";
    // ========================================================================//
    // Database Parameters
    // ========================================================================//
    public static String db_DriverClassName = "com.mysql.jdbc.Driver";
    public static String db_name = "";
    public static String db_address = "";
    public static String db_user = "";
    public static String db_password = "";
    public static String db_URL = "";
    public static String db_params = "useUnicode=true&characterEncoding=UTF-8";
    public static int db_MaxConnections = 3;
    /**
     * Contains the parameters and default values for this gateway such as
     * system id, password, default npi, and ton of sender...
     */
    private static Properties properties = new Properties();

    static {
    	try {
			loadProperties();
		} catch (Exception e) { 
			e.printStackTrace();
		}
    }
    /**
     * Loads configuration parameters from the file with the given name. Sets
     * private variable to the loaded values.
     */
    public static void loadProperties() throws Exception {
        FileInputStream propsFile = null;
        try {
            propsFile = new FileInputStream(fileName0);
            properties.load(propsFile);
            propsFile.close();
            db_address = properties.getProperty("serverAddress");
            db_name = properties.getProperty("databaseName");
            db_URL = "jdbc:mysql://" + db_address + "/" + db_name + "?" + db_params;
            db_user = properties.getProperty("userName");
            db_password = properties.getProperty("password");
            db_MaxConnections = getIntProperty("max_connections", db_MaxConnections);
        } catch (Exception e) {
        	// not found
            logger.error(e);
            db_address = "localhost:3306";
            db_name = "hrentry_db";
            db_URL = "jdbc:mysql://" + db_address + "/" + db_name + "?" + db_params;
            db_user = "root";
            db_password = "123456";
            db_MaxConnections = 5;
        }
    }

    public static void main(String arg[]) throws Exception {
        try {
            loadProperties();
            System.out.print("OK");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    // Gets a property and converts it into byte.
    static byte getByteProperty(String propName, byte defaultValue) {
        return Byte.parseByte(properties.getProperty(propName,
                Byte.toString(defaultValue)).trim());
    }

    // Gets a property and converts it into integer.
    static int getIntProperty(String propName, int defaultValue) {
        return Integer.parseInt(properties.getProperty(propName,
                Integer.toString(defaultValue)).trim());
    }
}
